package com.dzl.fee.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.fee.pojo.WelfareFeeUserCustomers;
import com.dzl.fee.service.WelfareFeeUserCustomersService;
import com.dzl.fee.dao.WelfareFeeUserCustomersSalesRelMapper;
import com.dzl.fee.pojo.WelfareFeeUserCustomersSalesRel;
import com.dzl.fee.pojo.query.WelfareFeeUserCustomersSalesRelQuery;
import com.dzl.fee.pojo.vo.WelfareFeeUserCustomersSalesRelVo;
import com.dzl.fee.service.WelfareFeeUserCustomersSalesRelService;
import com.dzl.fee.pojo.WelfareFeeUserSales;
import com.dzl.fee.service.WelfareFeeUserSalesService;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业务员和客户关联表(WelfareFeeUserCustomersSalesRel)表服务实现类
 *
 * @author wangjian
 * @date 2021-08-03 15:18:57
 */
@Service
public class WelfareFeeUserCustomersSalesRelServiceImpl extends BaseServiceImpl<WelfareFeeUserCustomersSalesRel> implements WelfareFeeUserCustomersSalesRelService {

    @Autowired
    private WelfareFeeUserCustomersSalesRelMapper mapper;
    @Autowired
    private WelfareFeeUserCustomersService welfareFeeUserCustomersService;
    @Autowired
    private WelfareFeeUserSalesService welfareFeeUserSalesService;

    @Override
    public Map<String, Object> selectCustomersSalesRelVoList(WelfareFeeUserCustomersSalesRelQuery query) {
        Map<String, Object> map = new HashMap<>();
        WelfareFeeUserCustomers welfareFeeUserCustomers = new WelfareFeeUserCustomers();
        welfareFeeUserCustomers.setStatus(1);
        welfareFeeUserCustomers.setEnterpriseId(query.getEnterpriseId());
        List<WelfareFeeUserCustomers> customersList = welfareFeeUserCustomersService.select(welfareFeeUserCustomers);
        WelfareFeeUserSales welfareFeeUserSales = new WelfareFeeUserSales();
        welfareFeeUserSales.setStatus(1);
        welfareFeeUserSales.setEnterpriseId(query.getEnterpriseId());
        List<WelfareFeeUserSales> salesList = welfareFeeUserSalesService.select(welfareFeeUserSales);
        List<WelfareFeeUserCustomersSalesRelVo> customersSalesRelVos = mapper.selectCustomersSalesRelVoList(query);
        if (EmptyUtils.isNotEmpty(query.getCustomersId()) && EmptyUtils.isNotEmpty(query.getSalesId())){
            WelfareFeeUserCustomersSalesRelQuery customersSalesRelQuery = new WelfareFeeUserCustomersSalesRelQuery();
            BeanUtils.copyProperties(query,customersSalesRelQuery);
            query.setCustomersId(null);
            List<WelfareFeeUserCustomersSalesRelVo> customersVo = mapper.selectCustomersSalesRelVoList(query);
            List<WelfareFeeUserCustomers> customersList1 = customersVo.stream().filter(x ->x.getWelfareFeeUserCustomers().getStatus()==1).map(WelfareFeeUserCustomersSalesRelVo::getWelfareFeeUserCustomers).collect(Collectors.toList());
            customersSalesRelQuery.setSalesId(null);
            List<WelfareFeeUserCustomersSalesRelVo> salesVos = mapper.selectCustomersSalesRelVoList(customersSalesRelQuery);
            List<WelfareFeeUserSales> salesList1 = salesVos.stream().filter(x ->x.getWelfareFeeUserSales().getStatus()==1).map(WelfareFeeUserCustomersSalesRelVo::getWelfareFeeUserSales).collect(Collectors.toList());
            map.put("customersList",customersList1);
            map.put("salesList",salesList1);
        }else if (EmptyUtils.isEmpty(query.getCustomersId()) && EmptyUtils.isEmpty(query.getSalesId())){
            map.put("customersList",customersList);
            map.put("salesList",salesList);
        }else if (EmptyUtils.isEmpty(query.getSalesId()) && EmptyUtils.isNotEmpty(query.getCustomersId())){
            List<WelfareFeeUserSales> salesList1 = customersSalesRelVos.stream().filter(x ->x.getWelfareFeeUserSales().getStatus()==1).map(WelfareFeeUserCustomersSalesRelVo::getWelfareFeeUserSales).collect(Collectors.toList());
            map.put("customersList",customersList);
            map.put("salesList",salesList1);
        }else if (EmptyUtils.isEmpty(query.getCustomersId()) && EmptyUtils.isNotEmpty(query.getSalesId())){
            List<WelfareFeeUserCustomers> customersList1 = customersSalesRelVos.stream().filter(x ->x.getWelfareFeeUserCustomers().getStatus()==1 ).map(WelfareFeeUserCustomersSalesRelVo::getWelfareFeeUserCustomers).collect(Collectors.toList());
            map.put("customersList",customersList1);
            map.put("salesList",salesList);
        }
        return map;
    }
}